package com.prime.controller;

import com.prime.common.MyException;
import com.prime.common.constants.ApiConstants;
import com.prime.entity.Demo;
import com.prime.service.DemoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cph on 17/8/29.
 */
@Controller
public class HelloController {

    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    private DemoService demoService;

    //页面
    @RequestMapping("/list")
    public String say(Model model) {
        Map map = new HashMap();
        List<Demo> demoList = demoService.findByWhere(map);
        model.addAttribute("demoList", demoList);
        return "Hello";
    }

    //检查选题的逻辑
    @GetMapping(value = "/get")
    public String checkTopic() throws Exception {
        try {
            String result = demoService.get(new HashMap<>());
            throw new Exception("发生错误");
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //检查选题的逻辑
    @GetMapping(value = "/testException")
    public String testException() {
        try {
            String result = demoService.get(new HashMap<>());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ApiConstants.ADD_F_MSG, e);
        }
    }

}
